package cms.com.yh.contorller;

import cms.common.Result;
import cms.mager.domain.Customer;
import cms.mager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @ClassName: CustomerController.java
 * @author: yuhui
 * @version: 1.0.0
 * @Description: TODO
 * @createTime: 2021年04月20日 00:46:00
 */
@Controller
@RequestMapping("cus")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 功能描述: 模糊分页查询所有客户信息
     * @Author: yuhui  2021/4/9 20:25
     */
    @RequestMapping("page.do")
    @ResponseBody                  //【注意：ResponseBody转换json格式 对 ModelAndView没有效果！！！！！】
    public Result PageInfo(String name, String phone, String company, Integer user_id, Integer deleted, Integer page, Integer limit, Model model) throws IOException {
        Customer param = new Customer();
        param.setName(name);
        param.setPhone(phone);
        param.setCompany(company);
        param.setUser_id(user_id);
        param.setDeleted(deleted);
        Result getpage = customerService.getpage(page,limit,param);
        return getpage;
        /*ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(JSONObject.toJSONString(getpage));
        modelAndView.setViewName("/WEB-INF/view/custo/cusList.jsp");
        return modelAndView;*/
    }

   /**
     * 功能描述: 添加客户
     * @Author: yuhui  2021/4/9 20:37
     */
    @RequestMapping("add.do")
    @ResponseBody
    public Result addInfo(String name,String phone,Integer sex,String company,String user_id, HttpServletResponse resp) throws IOException {
        Customer customer = paresParamForAdd(name,phone,sex,company,user_id);
        Result res = customerService.addData(customer);
        return res;
    }

    /**
     * 功能描述: 删除客户
     * @Author: yuhui  2021/4/9 20:38
     */
    @RequestMapping("del.do")
    @ResponseBody
    public Result delInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Result res = customerService.delData(Integer.valueOf(id));
        return res;
    }

    /**
     * 功能描述: 更新客户信息
     * @Author: yuhui  2021/4/9 20:40
     */
    @RequestMapping("upd.do")
    @ResponseBody
    public Result updInfo(Integer id,String name,String phone,Integer sex,String company) throws IOException {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setName(name);
        customer.setPhone(phone);
        customer.setSex(sex);
        customer.setCompany(company);
        customer.setUpdate_time(new Date());

        Result res = customerService.updateData(customer);
        return res;
    }

    /**
     * 功能描述: 更新客户的业务员
     * @Author: yuhui  2021/4/10 19:34
     */
    @RequestMapping("set.do")
    @ResponseBody
    public Result setUser(String[] cid,String user_c) throws IOException {
        Result result = customerService.setUserToCu(cid, user_c);
        return result;
    }



    private Customer paresParamForAdd(String name,String phone,Integer sex,String company,String user_id){

        Customer customer = new Customer();
        customer.setName(name);
        customer.setPhone(phone);
        customer.setSex(sex);
        customer.setCompany(company);
        customer.setUser_id(Integer.parseInt(user_id));
        return customer;
    }
}
